//系统配置-角色管理
App.pages.add((function(){
    var NS = Admin.Access.Role;
    var page = new App.Page({
        name: 'access/role/index'
    });
    //页面初始化
    page.initStack.add('dataTable', function(){
        //面包屑导航
        this.setBreadcrumb([{
            icon: '<i class="fa fa-folder"></i>',
            content: '系统配置'
        }, {
            content: '角色管理'
        }]);
        this.dataTable = new App.DataTable({
            baseUrl: '/admin/access/role',
            dataUrl: 'lists',
            floatable: true,
            paginator: false,
            table: {
                id: 'role_id',
                main: 0,
                showId: false,
                withSelect: false,
                columns: [
                    {name: '角色名称', value: 'name'},
                    {name: '修改时间', value: 'updated_at', width: '150px'}
                ],
                getItems: function(data, table){
                    return {
                        rootKeyLength: 100,
                        lists: [],
                        frame: table.frame,
                        init: function(data){
                            this.rootKeyLength = 100;
                            this.lists = [];
                            this.printTr(data);
                            this.frame.removeClass('table-striped');
                            var tbody = this.frame.find('tbody');
                            tbody.find('tr').remove();
                            for(var i in this.lists){
                                var tr = this.lists[i];
                                var id = tr.attr('data-id');
                                var spaceCount = (id.length - this.rootKeyLength)/2;
                                var html = '<span>';
                                for(var i=0;i<spaceCount;i++){
                                    html += '&nbsp;&nbsp;&nbsp;&nbsp;';
                                }
                                html += '</span>';
                                $(html).prependTo(tr.find('td').first());
                                tr.appendTo(tbody);
                            }
                            tbody.find('.down').each(function(){
                                $(this).click(function(){
                                    var selfId = $(this).parent().parent().attr('data-id');
                                    tbody.find('tr').each(function(){
                                        if($(this).attr('data-id') === selfId){
                                            $(this).find('.down').hide();
                                            $(this).find('.up').show();
                                            return ;
                                        }
                                        if($(this).attr('data-parent-id') === selfId){
                                            $(this).show();
                                            if($(this).hasClass('has-sub')){
                                                $(this).find('.up').hide();
                                                $(this).find('.down').show();
                                            }
                                        }
                                    });
                                });
                            });
                            tbody.find('.up').each(function(){
                                $(this).click(function(){
                                    var selfId = $(this).parent().parent().attr('data-id');
                                    tbody.find('tr').each(function(){
                                        if($(this).attr('data-id') === selfId){
                                            $(this).find('.up').hide();
                                            $(this).find('.down').show();
                                            return ;
                                        }
                                        if($(this).attr('data-id').indexOf(selfId) === 0){
                                            $(this).hide();
                                        }
                                    });
                                });
                            });
                        },
                        printTr: function(data){
                            for(var key in data){
                                this.lists.push(this.getTr(data[key]));
                                if(data[key]['sub']){
                                    this.printTr(data[key]['sub']);
                                }
                            }
                        },
                        getTr: function(data){
                            var roleId = data['role_id_code']+'';
                            var parentId = data['parent_id_code'];
                            var updateTime = data['updated_at'];
                            if(roleId.length < this.rootKeyLength){
                                this.rootKeyLength = roleId.length;
                            }
                            var tr = $([
                                '<tr data-id="'+roleId+'" data-parent-id="'+parentId+'">',
                                '   <td style="text-align:left;">',
                                '       <a href="javascript:void(0)" title="展开" class="down" style="display:none;">',
                                '           <i class="fa fa-plus"></i>',
                                '       </a>',
                                '       <a href="javascript:void(0)" title="收起" class="up" style="display:none;">',
                                '           <i class="fa fa-minus"></i>',
                                '       </a>',
                                '       <a href="javascript:void(0)" class="none" style="display:none;">',
                                '           <i class="fa fa-ban"></i>',
                                '       </a>',
                                '       '+data['name'],
                                '   </td>',
                                '   <td style="text-align:center;">',
                                '       '+App.util.formator.dateTime(updateTime),
                                '   </td>',
                                '</tr>'
                            ].join(''));
                            if(data.sub_count > 0){
                                tr.addClass('has-sub');
                                tr.find('.up').show();
                            }else{
                                tr.find('.none').show();
                            }
                            tr.find('td').first().addClass('main');
                            tr.attr('data-data', JSON.stringify(data));
                            return tr;
                        }
                    }.init(data);
                }
            },
            menu: [
                {parent: 'add', permission: 'op:admin.access.role.create'},
                {parent: 'view', permission: 'op:admin.access.role.detail'},
                {parent: 'edit', permission: 'op:admin.access.role.update'},
                {parent: 'drop', permission: 'op:admin.access.role.delete', operation: function(dataTable, data){ 
                    App.util.hint.confirm({
                        text: '删除角色会将该角色及其所有子角色一起删除，你确定要删除吗？',
                        yes: function(){
                            var id = dataTable.config.table.id;
                            var postData = {};
                            postData[id] = data[id];
                            dataTable.postData({
                                url: 'delete',
                                data: postData,
                                alert: '删除成功！',
                                reload: true
                            });
                        }
                    });
                }},
                {type: 'inline', permission: 'op:admin.access.role.changeOperations', name: '分配操作', operation: function(dataTable, data){
                    var win = dataTable.wins.get('operationManage');
                    //填充内容
                    App.util.request.getData({
                        url: dataTable.url('getOperations'),
                        data: {role_id: data.role_id},
                        success: function(roles){
                            win.setTitle('分配操作 - '+data.name);
                            win.printer.flush(roles);
                            win.contentContainer.find('input[name="role_id"]').val(data.role_id);
                            win.show();
                        }
                    });
                }},
                {type: 'inline', permission: 'op:admin.access.role.create', name: '添加子角色', operation: function(dataTable, data){
                    dataTable.detail.add();
                    dataTable.detail.form.add().reset().change({parent_id: data.role_id});
                    dataTable.detail.win.setTitle('添加子角色 - '+data.name).show();
                }},
                {parent: 'oplog', permission: 'op:admin.access.role.getOpLogs'}
            ],
            detail: {
                form: {
                    items: [
                        {type: 'hidden', name: 'role_id'},
                        {type: 'hidden', name: 'parent_id'},
                        {type: 'input', name: 'name', title: '角色名称', rule: {
                            type: 'str',
                            range: [1, 250]
                        }},
                        {type: 'textarea', name: 'description', title: '角色简介', rule: {
                            type: 'str',
                            range: [0, 250]
                        }}
                    ]
                }
            }, 
            after: function(dataTable){
                //分配操作
                dataTable.wins.add(new NS.OperationManage(dataTable), 'operationManage');
            },
            afterChange: function(){
                //通知
                App.pages.get('access/role/index').dirty();
            }
        }, page);
        this.dataTable.fix(this.contentContainer);
        this.dataTable.loadData();
    });
    //页面刷新
    page.refreshStack.add('dataTable', function(){
        this.dataTable.loadData(true);
    });
    //页面销毁
    page.destroyStack.add('dataTable', function(){
        this.dataTable.destroy();
    });
    return page;
})());